layui.define(['table', 'form','laydate','my_send'], function(exports){
    var $ = layui.$
    ,table = layui.table
    ,form = layui.form
    ,laydate = layui.laydate
    , http_proxy = layui.http_proxy;
    laydate.render({
        elem: '#firstRepairDate'
        ,trigger: 'click'
        ,position:'fixed'
    });
    function getDiff(s1,s2){
      var days = s2.getTime() - s1.getTime();
      var time = parseInt(days / (1000 * 60 * 60 * 24));
      return time;
    }
    laydate.render({
        elem: '#repairDate'
        ,trigger: 'click'
        ,position:'fixed'
        ,done: function(value, date, endDate){
          var nextRepairDate = new Date($("#nextRepairDate").val());
          var repairDate =  new Date(value);
          var DiffTime = getDiff(repairDate,nextRepairDate);
          $("#intervalDay").val(DiffTime);
        }
    });
    laydate.render({
        elem: '#nextRepairDate'
        ,trigger: 'click'
        ,position:'fixed'
         ,done: function(value, date, endDate){
          var nextRepairDate = new Date(value);
          var repairDate =  new Date($("#repairDate").val())
          var DiffTime = getDiff(repairDate,nextRepairDate);
          $("#intervalDay").val(DiffTime);
        }
    });
    $.ajax({
        url:http_proxy('/devStatus/getDomain'),
        async: false,
        dataType:'json',
        type:'get',
        success:function(data){
            $.each(data.result,function(index,item){
                var laValue = item.label+"#@***#!-"+item.value;
                $('#majorList').append(new Option(item.label,laValue));
            })
            form.render('select');
        }
    });
    // 系统名称
    form.on('select(majorListFilter)', function(data){
        var laValueArray = data.value.split("#@***#!-");
        $.ajax({
            url:http_proxy('/devStatus/getSystem?parentId=')+laValueArray[1],
            async: false,
            dataType:'json',
            type:'get',
            success:function(data){
                $("#systemList").empty();
                if(data.result != ""){
                    $("#systemList").append(new Option("请选择",""));
                    $.each(data.result,function(index,item){
                        var laValue = item.label+"#@***#!-"+item.value;
                        $('#systemList').append(new Option(item.label,laValue));
                    })
                }else{
                    $("#systemList").append(new Option("无数据",""));
                }
                form.render('select');
            }
        });
    })
    // 设备名称
    form.on('select(systemListFilter)', function(data){
        var laValueArray = data.value.split("#@***#!-");
        $.ajax({
            url:http_proxy('/devStatus/getDevice?parentId=')+laValueArray[1],
            async: false,
            dataType:'json',
            type:'get',
            success:function(data){
                $("#equipmentList").empty();
                if(data.result != ""){
                    $("#equipmentList").append(new Option("请选择",""));
                    $.each(data.result,function(index,item){
                        $('#equipmentList').append(new Option(item.label,item.laValue));
                    })
                }else{
                    $("#equipmentList").append(new Option("无数据",""));
                }
                form.render('select');
            }
        });
    })
     //车站下拉列表
  $.ajax({
    type : "get",
    async : false, //异步执行
    url : http_proxy("/devStatus/getStation"),
    dataType : "json", //返回数据形式为json
    success : function(data) {
      $.each(data.result,function(index,item){
        $('#stationList').append(new Option(item.label,item.label));
      })
      form.render('select');
    },
    error : function(errorMsg) {
      alert("请求数据失败");
    }
  });
  //处理状态
    $.ajax({
        type : "get",
        async : false, //异步执行
        url : http_proxy("/devStatus/getStatus"),
        dataType : "json", //返回数据形式为json
        success : function(data) {
            $.each(data.result,function(index,item){
                $('#statusList').append(new Option(item.label,item.value));
            })
            form.render('select');
        },
        error : function(errorMsg) {
            alert("请求数据失败");
        }
    });
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引  
    form.on('submit(layuiadmin-app-form-submit)', function(data){
        var field = data.field//获取提交的字段
         ,domainArray = field.doMain
         ,systemNameArray = field.systemName
         ,etNameArray = field.eventName
         ,chekDomain = domainArray.indexOf("#@***#!-")
         ,cheksystemName = systemNameArray.indexOf("#@***#!-")
         ,cheketName = etNameArray.indexOf("#@***#!-")
         ,domainStr = field.doMain
         ,systemNameStr = field.systemName
         ,etNameStr = field.eventName;
        if(chekDomain!=-1||chekDomain!='-1'){
            domainStr = domainArray.split("#@***#!-")[0];
        }
        if(cheksystemName!=-1||cheksystemName!='-1'){
            systemNameStr = systemNameArray.split("#@***#!-")[0];
        } 
        if(cheketName!=-1||cheketName!='-1'){
            etNameStr = etNameArray.split("#@***#!-")[0];
        }
        field.doMain = domainStr;
        field.systemName = systemNameStr;
        field.eventName = etNameStr;
        field.maintainWay = '计划维修';
        $.ajax({
            url:http_proxy('/et/maintain/addMaintain'),
            async: false,
            type:'post',
            contentType: 'application/json; charset=UTF-8',
            dataType:'json',
            data:JSON.stringify(field),
            success:function (data) {
                if(data.code = "success"){
                    layer.msg(data.message, {icon: 6});
                    // parent.layui.table.reload('planTableAll');
                }else{
                    layer.msg(data.message, {icon: 5});
                }
            }
        });
       parent.layer.close(index); //再执行关闭 
       parent.layui.table.reload('planTableAll');
    })
    form.on('submit(recordSubmit)', function(data){
        var field = data.field//获取提交的字段
        ,domainArray = field.domain
        ,systemNameArray = field.systemName
        ,etNameArray = field.eventName
        ,chekDomain = domainArray.indexOf("#@***#!-")
        ,cheksystemName = systemNameArray.indexOf("#@***#!-")
        ,cheketName = etNameArray.indexOf("#@***#!-")
        ,domainStr = field.domain
        ,systemNameStr = field.systemName
        ,etNameStr = field.eventName;
       if(chekDomain!=-1||chekDomain!='-1'){
           domainStr = domainArray.split("#@***#!-")[0];
       }
       if(cheksystemName!=-1||cheksystemName!='-1'){
           systemNameStr = systemNameArray.split("#@***#!-")[0];
       } 
       if(cheketName!=-1||cheketName!='-1'){
           etNameStr = etNameArray.split("#@***#!-")[0];
       }
       field.domain = domainStr;
       field.systemName = systemNameStr;
       field.eventName = etNameStr;
       field.maintainWay = '检修';
       $.ajax({
           url:http_proxy('/et/maintain/addRecord'),
           async: false,
            type:'post',
            contentType: 'application/json; charset=UTF-8',
            dataType:'json',
            data:JSON.stringify(field),
            success:function (data) {
                if(data.code = "success"){
                    layer.msg(data.message, {icon: 6});
                  
                }else{
                    layer.msg(data.message, {icon: 5});
                }
           }
       });
       parent.layer.close(index); //再执行关闭 
       parent.layui.table.reload('LAY-equipment-record'); //重载表格
    })
    exports('equipmentCom', {})
});